package com.itheima.tlias.Controller;

import com.itheima.tlias.Service.EmpService;
import com.itheima.tlias.pojo.Emp;
import com.itheima.tlias.pojo.Result;
import com.itheima.tlias.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping
    public Result Login(@RequestBody Emp emp) {
        log.info("登录" + emp);
        Emp e = empService.Login(emp);

        //登录成功生成令牌，下发令牌
        if (e != null) {
            Map<String, Object> claims = new HashMap<>();

            claims.put("id", e.getId());
            claims.put("username", e.getUsername());
            claims.put("password", e.getPassword());
            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        //登录失，返回错误信息
        else{
            return Result.error("用户名或密码错误！");
        }


    }
}
